package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.domain.City;
import com.sanjishu.enterlab.electricity.sale.domain.CompanyBak;
import com.sanjishu.enterlab.electricity.sale.domain.User;
import com.sanjishu.enterlab.electricity.sale.mapper.CityMapper;
import com.sanjishu.enterlab.electricity.sale.mapper.CompanyBakMapper;
import com.sanjishu.enterlab.electricity.sale.utils.Page;
import com.sanjishu.enterlab.electricity.sale.utils.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyBakService {
    @Autowired
    private CompanyBakMapper companyBakMapper;
    @Autowired
    private CityMapper cityMapper;
    @Value("${page.size}")
    private Integer pageSize;

    public CompanyBak getOneByName(String name) {
        return companyBakMapper.findByName(name);
    }

    public CompanyBak getById(Integer id) {
        return companyBakMapper.selectByPrimaryKey(id);
    }

    public void updateNoCity() {
        List<City> cityList = cityMapper.getArea(2);
        Map<String, Integer> maps = new HashMap<>();
        if (cityList != null && cityList.size()>0) {
            for(City city: cityList) {
                maps.put(city.getName(), city.getId());

            }
        }
        List<CompanyBak> companyBakList = companyBakMapper.findByAddress();
        if (companyBakList != null && companyBakList.size()>0) {
            for (CompanyBak companyBak:companyBakList) {
                if (maps.keySet() != null) {
                    for (String s: maps.keySet()){
                        if (companyBak.getCompanyName().contains(s))  {
                            companyBak.setCityId(maps.get(s));
                            companyBakMapper.updateByPrimaryKey(companyBak);
                        }
                    }
                }
            }
        }
    }

    public Page<CompanyBak> findCompanyBakByPage(Integer userId, Integer userTypes, String searchText,
                                                 Integer searchTypes, String columnName, Integer sortType, Integer page) {
        Long count = null;
        if (searchText != null && !searchText.equals("") && userTypes == User.TYPE_SALE) {
            count = companyBakMapper.countBySearchText(searchText, searchTypes);
        } else {
            count = companyBakMapper.countByRoles(searchText, searchTypes, userId);
        }
        if (count == 0) {
            return new Page<CompanyBak>(new PageRequest(1, pageSize), new ArrayList<>(), count);
        }
        PageRequest request = new PageRequest(page, pageSize);
        List<CompanyBak> result = null;
        if (searchText != null && !searchText.equals("") && userTypes == User.TYPE_SALE) {
            result = companyBakMapper.findBySearchText(searchText, searchTypes, columnName, sortType, request);
        } else {
            result = companyBakMapper.findByRoles(searchText, searchTypes, columnName, sortType, userId, request);
        }
        Page<CompanyBak> pageData = new Page<CompanyBak>(request, result, count);
        if (pageData.getCurrentPage() > pageData.getTotalPage()) {
            request = new PageRequest(pageData.getTotalPage(), pageSize);
            if (searchText != null && !searchText.equals("") && userTypes == User.TYPE_SALE) {
                result = companyBakMapper.findBySearchText(searchText, searchTypes, columnName, sortType, request);
            } else {
                result = companyBakMapper.findByRoles(searchText, searchTypes, columnName, sortType, userId, request);
            }
            pageData = new Page<>(request, result, count);
        }
        return pageData;
    }

    public Page<CompanyBak> findSaleConnects(String searchText, String columnName, Integer sortType, Integer page) {
        Long count = count = companyBakMapper.countSaleConnects(searchText);
        if (count == 0) {
            return new Page<CompanyBak>(new PageRequest(1, pageSize), new ArrayList<>(), count);
        }
        PageRequest request = new PageRequest(page, pageSize);
        List<CompanyBak> result = result = companyBakMapper.findSaleConnects(searchText, columnName, sortType, request);
        Page<CompanyBak> pageData = new Page<CompanyBak>(request, result, count);
        if (pageData.getCurrentPage() > pageData.getTotalPage()) {
            request = new PageRequest(pageData.getTotalPage(), pageSize);
            result = companyBakMapper.findSaleConnects(searchText, columnName, sortType, request);
            pageData = new Page<>(request, result, count);
        }
        return pageData;
    }

}
